package com.bawei.module_moto_circle.model

import android.content.Context
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import com.bawei.lib_common.appContext
import com.bawei.lib_common.widget.nine_pic_grid.Image

/**
 *作者：lxy
 *功能:
 *时间：2025/6/4
 */
class CommentModel(
    val current: Int,
    val pages: Int,
    val records: List<CommentInfo>,
    val size: Int,
    val total: Int
)

data class CommentInfo(
    val _like: Boolean,
    val be_reply_id: String,
    val channel: Int,
    val comment_number: Int,
    val content: String,
    val create_by: String,
    val create_name: String,
    val create_time: String,
    val del_flag: Boolean,
    val first_comment_id: String,
    val headImg: HeadImg,
    val head_img_id: String,
    val if_author: Boolean,
    val if_sensitive: Boolean,
    val is_like: Boolean,
    var level: Int,
    val like_number: Int,
    val model_name: Any,
    val model_nickname: Any,
    val oneself: Boolean,
    val parent_channel: Any,
    val parent_content: Any,
    val parent_id: String,
    val pinned: Boolean,
    val posts_comment_id: String,
    val posts_id: String,
    val reply_name: String,
    val twoLevel: List<CommentInfo>
){
    fun requireAvatarUrl(): String? {
        return if (isOfficial()) {
            null
        } else {
            headImg?.url
        }
    }

    fun requireCreateName(): String {
        return if (isOfficial()) {
            appContext.getString(com.bawei.lib_common.R.string.string_cyclone_official)
        } else {
            create_name ?: "未知用户"
        }
    }

    fun requireReplay(): String {
        return if (isParentOfficial()) {
            appContext.getString(com.bawei.lib_common.R.string.string_cyclone_official)
        } else {
            create_name ?: "未知用户"
        }
    }

    fun isOfficial() = channel == 1

    private fun isParentOfficial() = parent_channel == 1

    fun isPin(): Boolean = pinned == true


    fun formatReplayContent(content: String, replyName: String?, context: Context): CharSequence {
        val prefix = String.format("%s", replyName ?: "未知用户")
        val formatContent = String.format("回复%s：%s", prefix, content)
        val start = formatContent.indexOf(prefix)
        val end = start + prefix.length
        val spannableStringBuilder = SpannableStringBuilder(formatContent)
        spannableStringBuilder.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, com.bawei.lib_common.R.color.color_919191)),
            start,
            end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return spannableStringBuilder
    }
}

data class HeadImg(
    val contentLength: Any,
    val contentType: Any,
    val filename: String,
    val firstImage: Any,
    val id: Any,
    val imageHeight: Any,
    val imageWidth: Any,
    val thumbnailUrl: Any,
    val url: String
)



